package com.cjm.nio.demo;

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class Handler implements Runnable {
    private static final byte[] b = "hello, 服务器收到了你的信息.".getBytes(StandardCharsets.UTF_8);
    private SocketChannel sc;
    private ByteBuffer reqBuffer;
    private SubReactorThread parent;
    public Handler(SocketChannel sc, ByteBuffer reqBuffer, SubReactorThread subReactorThread) {
        super();
        this.sc = sc;
        this.reqBuffer = reqBuffer;
        this.parent = subReactorThread;
    }

    @Override
    public void run() {
        System.err.println("业务在handler中开始执行...");
        reqBuffer.put(b);
        parent.register(new NioTask(sc, SelectionKey.OP_WRITE, reqBuffer));
        System.err.println("业务在handler中执行结束.");
    }
}
